<?php

namespace app\common\controller\zhongzhuanv2;

use app\admin\library\Auth;
use think\Config;
use think\Controller;
use think\Db;
use baiying\lib\BYTokenClient;

/**
 * 后台控制器基类
 */
class Autophonetongbu1 extends Controller{


    public static function autophonetongbubycallinstanceid($request_data)
    {
        $where = empty($request_data['where']) ? '1=1' : $request_data['where'];
        $access_token = $request_data['access_token'];
        $companyId = $request_data['companyId'];

        $request_callInstanceId = empty($request_data['callInstanceId']) ? '0' : $request_data['callInstanceId'];
        $item_param = ['callInstanceId'=>$request_callInstanceId];
        $response = self::getAiTaskPhoneList($item_param,$access_token,$companyId);
        self::taskPhoneInsert($response['data']);
    }


    public static function getAiTaskPhoneList($request_data,$access_token,$companyId) {

        $client = new BYTokenClient($access_token);

        $method = 'byai.openapi.callinstance.detail.get'; //要调用的api名称
        $api_version = '1.0.0'; //要调用的api版本号


        $my_params = ['companyId' => $companyId];
        $my_params = array_merge($my_params,$request_data);
        $my_files = [];

        $result = $client->post($method, $api_version, $my_params, $my_files);

        return $result;

    }


    public static function taskPhoneInsert($data){
        if(empty($data)) return false;

        $save_data = [];
        $is_exist = db(getBaiYingDataTable())->where('callInstanceId',$data['callInstance']['callInstanceId'])->count();
        if(!empty($is_exist)) return false;
        $save_data = [
            'origin'=>1,
            'is_yixiang'=>0,
            'callInstanceId'=>$data['callInstance']['callInstanceId'],
            'callJobId'=>$data['callInstance']['callJobId'],
            'customerTelephone'=>$data['callInstance']['customerTelephone'],
            'customerName'=>$data['callInstance']['customerName'],
            'startTime'=>$data['callInstance']['startTime'],
            'endTime'=>$data['callInstance']['endTime'],
            'duration'=>$data['callInstance']['duration'],
            'luyinOssUrl'=>$data['callInstance']['luyinOssUrl'],
        ];
        
        $taskresult = $data['taskResult'];
        $yixiang_value = ['A'=>'A级(有明确意向)','B'=>'B级(可能有意向)','C'=>'C级','D'=>'D级(用户忙)'];
        foreach ($taskresult as $item) {
            if($item['resultName'] == '客户意向等级'){
                $save_data['is_yixiang'] = 1;
                $save_data['yixiang_txt'] = $yixiang_value[$item['resultValue']];
                $save_data['yixiang_value'] = $item['resultValue'];
            }
            
        }

        $save_data['config'] = json_encode($save_data,JSON_UNESCAPED_UNICODE);
        $save_data['createtime'] = time();

        if(empty($save_data)) return false;

        db(getBaiYingDataTable())->insertGetId($save_data);




    }


}
